package com.example.mock;

import static org.springframework.web.reactive.function.server.RequestPredicates.path;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
import static org.springframework.web.reactive.function.server.ServerResponse.ok;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

/**
 * @author Aaron
 */
@SpringBootApplication
public class MockApplication {

  public static void main(String[] args) {
    SpringApplication.run(MockApplication.class, args);
  }

  @Bean
  RouterFunction<ServerResponse> defaultRouter() {
    return route(path("/ok/**"), this::success);
  }

  Mono<ServerResponse> success(ServerRequest request) {
    String routeId = request.headers().firstHeader("x-benchmark-routeId");
    return ok()
        .header("x-mock-server-routeId", routeId)
        .contentType(MediaType.TEXT_PLAIN)
        .body(Mono.just(String.valueOf(routeId)), String.class);
  }
}
